////////////////////////////////////////////////////////////////////////////
//
//  Crytek Engine Source File.
//  Copyright (C), Crytek Studios, 2002.
// -------------------------------------------------------------------------
//  File name:   CommandPlayerFmodEx400.cpp
//  Version:     v1.00
//  Created:     8/11/2006 by Tomas
//  Compilers:   Visual Studio.NET
//  Description: FmodEx 4.00 Implementation of a CommandPlayer
// -------------------------------------------------------------------------
//  History:
//
////////////////////////////////////////////////////////////////////////////
#include "StdAfx.h"
#include "CommandPlayerFmodEx400.h"

char CCommandPlayerFmodEx400::m_sCommand[FMOD_COMMAND_PLAYER_MAX_COMMANDS];

#ifdef SOUNDSYSTEM_USE_FMODEX400
#define IS_FMODERROR (m_ExResult != FMOD_OK )

//////////////////////////////////////////////////////////////////////////
CCommandPlayerFmodEx400::CCommandPlayerFmodEx400()
{
	m_pFile        = NULL;
	m_ExResult     = FMOD_OK;
	m_nSpeakerMode = FMOD_SPEAKERMODE_RAW;
	m_pEventSystem = NULL;
	m_pFMODEX      = NULL;
	m_bRecording   = false;
}

//////////////////////////////////////////////////////////////////////////
CCommandPlayerFmodEx400::~CCommandPlayerFmodEx400()
{
	if (m_bRecording)
	{
		sprintf(m_sCommand,"</CommandPlayer>\n");
		fputs(m_sCommand, m_pFile);
		fflush(m_pFile);
	}

	if(m_pFile)
		fclose(m_pFile);
}

//////////////////////////////////////////////////////////////////////////
void CCommandPlayerFmodEx400::RecordFile(const char* sFilename)
{
	if (*sFilename!=0)
		m_pFile = fopen(sFilename,"wt");

	if (m_pFile)
	{
		sprintf(m_sCommand,"<CommandPlayer>\n");
		fputs(m_sCommand, m_pFile);
		fflush(m_pFile);
		m_bRecording = true;
	}
}

//////////////////////////////////////////////////////////////////////////
void CCommandPlayerFmodEx400::LogCommand(eCOMMANDS const command, int const nMilliSecs, ptParam const& rParam1 /* = ptParamUINT32 */, ptParam const& rParam2 /* = ptParamUINT32 */, ptParam const& rParam3 /* = ptParamUINT32 */, ptParam const& rParam4 /* = ptParamUINT32 */, ptParam const& rParam5 /* = ptParamUINT32 */)
{
	if (m_pFile && m_bRecording)
	{
		uint32 nParam1      = 0;
		uint32 nParam2      = 0;
		uint32 nParam3      = 0;
		uint32 nParam4      = 0;
		float fParam1       = 0.0f;
		float fParam2       = 0.0f;
		float fParam3       = 0.0f;
		char const* sParam1 = NULL;
		char const* sParam2 = NULL;
		Vec3 vParam2(0.0f);
		Vec3 vParam3(0.0f);
		Vec3 vParam4(0.0f);
		Vec3 vParam5(0.0f);

		rParam1.GetValue(nParam1);
		rParam1.GetValue(fParam1);
		rParam1.GetValue(sParam1);

		rParam2.GetValue(nParam2);
		rParam2.GetValue(fParam2);
		rParam2.GetValue(sParam2);
		rParam2.GetValue(vParam2);

		rParam3.GetValue(nParam3);
		rParam3.GetValue(fParam3);
		rParam3.GetValue(vParam3);

		rParam4.GetValue(nParam4);
		rParam4.GetValue(vParam4);

		rParam5.GetValue(vParam5);

		int nTime = nMilliSecs;

		switch (command)
		{
		case SYSTEM_:
			break;
		case SYSTEM_SETOUTPUT:
			sprintf(m_sCommand,"<Command ID=\"%d\" T=\"%d\" Output=\"%d\" />\n", command, nTime, nParam1);
			break;
		case SYSTEM_SETDRIVER:
			break;
			sprintf(m_sCommand,"<Command ID=\"%d\" T=\"%d\" Driver=\"%d\" />\n", command, nTime, nParam1);
		case SYSTEM_SETRECORDDRIVER:
			sprintf(m_sCommand,"<Command ID=\"%d\" T=\"%d\" Record=\"%d\" />\n", command, nTime, nParam1);
			break;
		case SYSTEM_SETHARDWARECHANNELS:
			sprintf(m_sCommand,"<Command ID=\"%d\" T=\"%d\" Min2D=\"%d\" Max2D=\"%d\" Min3D=\"%d\" Max3D=\"%d\" />\n", command, nTime, nParam1, nParam2, nParam3, nParam4);
			break;
		case SYSTEM_SETSPEAKERMODE:
			sprintf(m_sCommand,"<Command ID=\"%d\" T=\"%d\" Speaker=\"%d\" />\n", command, nTime, nParam1);
			break;
		case SYSTEM_SET3DSETTINGS:
			sprintf(m_sCommand,"<Command ID=\"%d\" T=\"%d\" Doppler=\"%f\" Distance=\"%f\" Rolloff=\"%f\" />\n", command, nTime, fParam1, fParam2, fParam3);
			break;
		case EVENTSYSTEM_CREATE: 
			sprintf(m_sCommand,"<Command ID=\"%d\" T=\"%d\" />\n", command, nTime);
			break;
		case EVENTSYSTEM_INIT: 
			sprintf(m_sCommand,"<Command ID=\"%d\" T=\"%d\" Channels=\"%d\" Flags=\"%d\" Extra=\"%d\" EventFlags=\"%d\" />\n", command, nTime, nParam1, nParam2, nParam3, nParam4);
			break;	
		case EVENTSYSTEM_UPDATE:
			sprintf(m_sCommand,"<Command ID=\"%d\" T=\"%d\" />\n", command, nTime);
			break;
		case EVENTSYSTEM_SET3DNUMLISTENERS: 
			sprintf(m_sCommand,"<Command ID=\"%d\" T=\"%d\" NumList=\"%d\" />\n", command, nTime, nParam1);
			break;	
		case EVENTSYSTEM_SET3DLISTENERSATTRIBUTES: 
			sprintf(m_sCommand,"<Command ID=\"%d\" T=\"%d\" ListID=\"%d\" PosX=\"%f\" PosY=\"%f\" PosZ=\"%f\" VelX=\"%f\" VelY=\"%f\" VelZ=\"%f\" ForwardX=\"%f\" ForwardY=\"%f\" ForwardZ=\"%f\" TopX=\"%f\" TopY=\"%f\" TopZ=\"%f\"  />\n", command, nTime, nParam1, 
				vParam2.x, vParam2.y, vParam2.z, 
				vParam3.x, vParam3.y, vParam3.z,
				vParam4.x, vParam4.y, vParam4.z,
				vParam5.x, vParam5.y, vParam5.z);
			break;	
		case EVENTSYSTEM_LOAD:
			sprintf(m_sCommand,"<Command ID=\"%d\" T=\"%d\" sProject=\"%s\" iProject=\"%d\" />\n", command, nTime, sParam1, nParam2);
			break;
		case EVENTSYSTEM_UNLOAD:
			break;
		case EVENTSYSTEM_RELEASE:
			break;
		case EVENTSYSTEM_GETSYSTEMOBJECT:
			sprintf(m_sCommand,"<Command ID=\"%d\" T=\"%d\" />\n", command, nTime);
			break;
		case EVENTSYSTEM_SETMEDIAPATH:
			sprintf(m_sCommand,"<Command ID=\"%d\" T=\"%d\" sMediaPath=\"%s\" />\n", command, nTime, sParam1);
			break;	
		case PROJECT_GETGROUP:
			sprintf(m_sCommand,"<Command ID=\"%d\" T=\"%d\" iProject=\"%d\" sGroup=\"%s\" Cache=\"%d\" iGroup=\"%d\" />\n", command, nTime, nParam1, sParam2, nParam3, nParam4);
			break;
		case GROUP_GETEVENT:
			sprintf(m_sCommand,"<Command ID=\"%d\" T=\"%d\" iGroup=\"%d\" sEvent=\"%s\" Mode=\"%d\" iEvent=\"%d\" />\n", command, nTime, nParam1, sParam2, nParam3, nParam4);
			break;
		case GROUP_FREEDATA:
			sprintf(m_sCommand,"<Command ID=\"%d\" T=\"%d\" iGroup=\"%d\" />\n", command, nTime, nParam1);
			break;
		case GROUP_FREEEVENTDATA:
			sprintf(m_sCommand,"<Command ID=\"%d\" T=\"%d\" iGroup=\"%d\" Idx=\"%d\" />\n", command, nTime, nParam1, nParam2);
			break;
		case EVENT_PLAY:
			sprintf(m_sCommand,"<Command ID=\"%d\" T=\"%d\" iEvent=\"%d\" />\n", command, nTime, nParam1);
			break;
		case EVENT_STOP:
			sprintf(m_sCommand,"<Command ID=\"%d\" T=\"%d\" iEvent=\"%d\" />\n", command, nTime, nParam1);
			break;
		case EVENT_SETPAUSED:
			sprintf(m_sCommand,"<Command ID=\"%d\" T=\"%d\" iEvent=\"%d\" Paused=\"%d\" />\n", command, nTime, nParam1, nParam2);
			break;
		case EVENT_SETVOLUME:
			sprintf(m_sCommand,"<Command ID=\"%d\" T=\"%d\" iEvent=\"%d\" Val=\"%f\" />\n", command, nTime, nParam1, fParam2);
			break;
		case EVENT_SET3DATTRIBUTES:
			sprintf(m_sCommand,"<Command ID=\"%d\" T=\"%d\" iEvent=\"%d\" PosX=\"%f\" PosY=\"%f\" PosZ=\"%f\" VelX=\"%f\" VelY=\"%f\" VelZ=\"%f\" />\n", command, nTime, nParam1, vParam2.x, vParam2.y, vParam2.z, vParam3.x, vParam3.y, vParam3.z);
			break;
		case EVENT_GETPARAMETER:
			sprintf(m_sCommand,"<Command ID=\"%d\" T=\"%d\" iEvent=\"%d\" sParam=\"%s\" iParam=\"%d\" />\n", command, nTime, nParam1, sParam2, nParam3);
			break;
		case PARAMETER_SETVALUE:
			sprintf(m_sCommand,"<Command ID=\"%d\" T=\"%d\" iParam=\"%d\" Val=\"%f\" />\n", command, nTime, nParam1, fParam2);
			break;
		default:
			{
				break;
			}
		}

		fputs(m_sCommand, m_pFile);
		fflush(m_pFile);
	}
}

#endif // SOUNDSYSTEM_USE_FMODEX400
